package com.java.util;

import com.java.util.pojo.User;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * List集合排序相关
 */
public class ListSort {

    public static void main(String[] args) {
        List<User> list = new ArrayList<>();

        list.add(new User(1, "c", "2", null, true));
        list.add(new User(3, "c", "3", 14, false));
        list.add(new User(2, "c", "6", 32, false));

        // 先根据 name排序，如果name为空则根据city排序，null排序放在最后Comparator.nullsLast
        List<User> collect = list.stream()
                .sorted(Comparator.comparing(User::getName, Comparator.nullsLast(String::compareTo))
                        .thenComparing(User::getCity, Comparator.nullsLast(String::compareTo)))
                .collect(Collectors.toList());


//        List<AdComponentDetailDto> sortedList = adAssetVoList.stream()
//                .sorted(Comparator.comparing(AdComponentDetailDto::getDisplayName).reversed()
//                        .thenComparing(AdComponentDetailDto::getVariant, Comparator.nullsLast(Comparator.reverseOrder())))
//                .collect(Collectors.toList());

        collect.stream().forEach(System.out::println);

        // 使用Comparator.comparingInt排整数
        list.sort(Comparator.comparingInt(User::getAssetId));
        // 使用getAssetId排序，如果有null值，不会报错放在最后
        list.sort(Comparator.comparing(User::getAssetId, Comparator.nullsLast(Comparator.naturalOrder())));

    }
}
